package com.hm.health.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.goodix.view.SmoothCheckBox;
import com.hm.health.R;
import com.hm.health.entity.CheckBoxProperty;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by chenshi on 2019/12/29.
 */

public class CheckBoxPropertyAdapter extends RecyclerView.Adapter<CheckBoxPropertyAdapter.ViewHolder>{
    private Context mContext;
    private List<CheckBoxProperty> mCheckBoxProperties;
    private SmoothCheckBox.OnCheckedChangeListener mOnCheckedChangeListener;

    public CheckBoxPropertyAdapter(Context context, List<CheckBoxProperty> checkBoxProperties) {
        mContext = context;
        mCheckBoxProperties = checkBoxProperties;
    }

    public  void setOnCheckedChangeListener(SmoothCheckBox.OnCheckedChangeListener listener) {
        mOnCheckedChangeListener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_checkbox, null);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
       if (position >= mCheckBoxProperties.size()) {
           return;
       }

        CheckBoxProperty property = mCheckBoxProperties.get(position);
        viewHolder.mTextView.setText(property.getText());
        viewHolder.mSmoothCheckBox.setUnCheckedColor(property.getColorUnchecked());
        viewHolder.mSmoothCheckBox.setCheckedColor(property.getColorChecked());
        viewHolder.mSmoothCheckBox.setChecked(property.isChecked());
        viewHolder.mSmoothCheckBox.setOnCheckedChangeListener((checkBox, isChecked) -> {
            property.setChecked(viewHolder.mSmoothCheckBox.isChecked());
            if (null != mOnCheckedChangeListener) {
                mOnCheckedChangeListener.onCheckedChanged(checkBox, isChecked);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (null == mCheckBoxProperties) {
            return 0;
        }
        return mCheckBoxProperties.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.scb_checkbox)
        SmoothCheckBox mSmoothCheckBox;
        @BindView(R.id.tv_text)
        TextView mTextView;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
